
import java.io.*;
import java.net.*;
import java.sql.SQLException;

/**
 *
 * @author CNTT
 * @description
 * Lay Thong Tin Gia Vang Tu Tran
 * http://www.sjc.com.vn/ajax_vang.php
 * Cap nhat vao co so du lieu
 */
public class GiaVang {

    private URL url;
    //khoi tao url full

    public GiaVang() {
        try {
            this.url = new URL("http://www.sjc.com.vn/ajax_vang.php");
        } catch (MalformedURLException e) {
            System.err.println(e);
        }
    }
    //khoi tao url chi tiet

    public GiaVang(String prot, String host, String port, String file) {
        try {
            if (port.length() > 0) {
                this.url = new URL(prot, host, Integer.valueOf(port), file);
            } else {
                this.url = new URL(prot, host, file);
            }
        } catch (MalformedURLException e) {
            System.err.println(e);
        }
    }
    //kiem tra xem thu url da dc xac dinh chua

    public boolean isURL() {
        if (url == null) {
            return false;
        }
        return true;
    }
    //xuat protocol

    public String getProtocol() {
        return url.getProtocol();
    }
    //xuat port

    public int getPort() {
        int i = url.getPort();
        if (i == -1) {
            i = url.getDefaultPort();
        }
        return i;
    }
    //xuat host

    public String getHost() {
        return url.getHost();
    }
    //xuat file name

    public String getFile() {
        return url.getFile();
    }
    //xuat duong dan

    public String getPath() {
        return url.getPath();
    }
    //xuat url

    public String getURL() {
        return this.getProtocol() + "://"
                + this.getHost()
                + this.getFile() + ":" + String.valueOf(this.getPort());
    }

    /*
     * @description:Lay ngay cap nhat gia vang
     * @resource:html
     * @return:String dd/mm/yy hh:mm
     */
    public String layNgay(String s) {
        //String k=new String();
        String dau = "Cập nhật lúc ";
        String cuoi = "<br />";
        if (s.lastIndexOf(dau) > -1) {
            s = s.substring(s.lastIndexOf(dau) + dau.length());
            s = s.substring(0, s.lastIndexOf(cuoi));
            //Doi lai dinh dang hh:mm dd/mm/yy -> dd/mm/yy hh:mm
            int idx = s.lastIndexOf(' ');
            s = s.substring(idx + 1) + ' ' + s.substring(0, s.lastIndexOf(' '));
            return s;
        }
        return null;

    }

    //Lay khu vuc cap nhat gia vang (html)
    public String layKhuVuc(String s) {
        char nhay = 34;
        String dau = "class=" + nhay + "text_tgvang_tp_down" + nhay + ">";
        String cuoi = "</a>";
        if (s.lastIndexOf(dau) > -1) {
            s = s.substring(s.lastIndexOf(dau) + dau.length());
            s = s.substring(0, s.indexOf(cuoi));
            return s;
        }

        dau = "class=" + nhay + "text_tgvang_tp_up" + nhay + ">";
        cuoi = "</a>";
        if (s.lastIndexOf(dau) > -1) {
            s = s.substring(s.lastIndexOf(dau) + dau.length());
            s = s.substring(0, s.indexOf(cuoi));
            return s;
        }
        return null;
    }
    //Doc thong tin ve Loai Vang,Gia Ban,Gia Mua

    public String docChiTiet(String s) {
        char nhay = 34;
        String x = String.valueOf(nhay);
        String dau = "<td width=" + x + "38%" + x + " align=" + x + "right" + x + " class=" + x + "text_tgvang_chitiet" + x + "><strong>";
        String cuoi = "</strong>";
        if (s.lastIndexOf(dau) > -1) {
            s = s.substring(s.lastIndexOf(dau) + dau.length());
            s = s.substring(0, s.indexOf(cuoi));
            return s;
        }
        dau = "<td width=" + x + "25%" + x + " align=" + x + "right" + x + " class=" + x + "text_tgvang_chitiet" + x + ">";
        cuoi = "</td>";
        if (s.lastIndexOf(dau) > -1) {
            s = s.substring(s.lastIndexOf(dau) + dau.length());
            s = s.substring(0, s.indexOf(cuoi));
            return s;
        }

        dau = "<td width=" + x + "28%" + x + " align=" + x + "right" + x + " class=" + x + "text_tgvang_chitiet" + x + ">";
        cuoi = "&nbsp;";
        if (s.lastIndexOf(dau) > -1) {
            s = s.substring(s.lastIndexOf(dau) + dau.length());
            s = s.substring(0, s.indexOf(cuoi));
            return s;
        }

        return null;
    }

    //
    //@description Cap nhat gia vang tu Internet vao co so du lieu
    //@return result
    //
    public int capnhatGiaVang() throws Exception {

        DbSqlite db = new DbSqlite("mydatabase.db");
        db.update("create table if not exists GIAVANG(KHUVUC,LOAI,GIABAN,GIAMUA,THOIGIAN,PRIMARY KEY(KHUVUC,LOAI,THOIGIAN));");

        StringBuilder sb = new StringBuilder();
        DataInputStream dis = null;
        BufferedReader br = null;

        try {
            dis = new DataInputStream(url.openStream());
            br = new BufferedReader(new InputStreamReader(dis, "utf-8"));
        } catch (IOException ex) {
            System.out.println(ex.toString());
        }
        if (dis == null) {
            System.out.println("Not Connect to internet");
            return 1;
        }
        String ngaygio = new String();
        String khuvuc = new String();
        String st = new String();
        byte i = 0;
        String tam = new String();
        //  String a[] = new String[5];

        //Lay ngay gio cap nhat
        while ((st = br.readLine()) != null) {
            ngaygio = this.layNgay(st);
            khuvuc = this.layKhuVuc(st);
            break;
        }

        //Lay cac thong tin ve gia vang
        while ((st = br.readLine()) != null) {
            tam = this.docChiTiet(st);
            if (tam != null) {
                if (sb.length() == 0) {
                    sb.append(khuvuc + ",");
                }
                ++i;
                sb.append(tam);
                sb.append(",");
                if (i % 3 == 0) {
                    sb.append(ngaygio);
                    try {
                        db.insert("GIAVANG", sb.toString());
                    } catch (SQLException ex) {
                        System.out.println(ex.toString());
                    }
                    sb.delete(0, sb.length());
                    i = 0;
                }
            } else {
                if (this.layKhuVuc(st) != null) {
                    khuvuc = this.layKhuVuc(st);
                }
            }
        }
        //Dong co so du lieu
        db.close();
        return 0;
    }
}
